
package uestc.bitman.edxapp.mainui;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.EdxStudentUser;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainUIActivity extends Activity {

    IActiviyController mController;

    public static String TAG_ACTION = "action";
    
    boolean bContinue = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdxApp.createEdxStudentUser("", "");
        EdxStudentUser user = EdxApp.getEdxAppUser();
        user.loadSavedData(this);
        Intent intent = getIntent();
        String action = intent.getStringExtra(TAG_ACTION);
        if (action != null && !action.equals("")) {
            mController = MainActivityControllerFactory.forActivity(action);
        } else {
            if (user.getSavedLoginStatus(this)) {
                mController = new MainUIController();
            }else{
                mController = new LoginUIController();
            }
        }
        mController.initController(this);
        mController.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mController.onPostCreate(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mController.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mController.onCreateOptionsMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        mController.onPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        if(!mController.onBackPressed()){
            super.onBackPressed();
        }
    }
    
    public void enablePressAgainToExit(boolean b){
        if(mController instanceof MainUIController){
            ((MainUIController)mController).enablePressAgainToExit(b);
        }
    }
}
